//
//  YBApiManager+Friend.m
//  ChangChat
//
//  Created by Cocoa on 2019/9/2.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "YBApiManager+Friend.h"

@implementation YBApiManager (Friend)
- (RACSignal *)getFriendList {
    return [self sendGetRequest:@"friend/my_friends" param:nil];
}
- (RACSignal *)getFriend_groupList {
    return [self sendGetRequest:@"index/my_sort" param:nil];
}
- (RACSignal *)getNewFriendList {
    return [self sendGetRequest:@"friend/request_index" param:nil];
}
- (RACSignal *)addOrEditAddressGroupWithName:(NSString *)name sortID:(NSString *)sort_id {
    NSDictionary *param = nil;
    if (sort_id.isHaveStr == YES) {
        param = @{@"sort_id": sort_id,
                  @"name": name
                   };
    } else {
        param = @{@"name": name};
    }
    return [self sendGetRequest:@"index/edit_sort" param:param];
}
- (RACSignal *)deleteAddressGroupWithSortID:(NSString *)sort_id {
    NSDictionary *param = @{@"sort_id": sort_id};
    return [self sendGetRequest:@"index/del_sort" param:param];
}
- (RACSignal *)moveFriendToNewGroupWithSortID:(NSString *)sort_id friend_id:(NSString *)friend_id {
    NSDictionary *param = @{@"sort_id": sort_id,
                            @"friend_id": friend_id
                            };
    return [self sendGetRequest:@"index/move_to_sort" param:param];
}
- (RACSignal *)moveFriendToBlacklistWithFriend_id:(NSString *)friend_id {
    NSDictionary *param = @{@"friend_id": friend_id};
    return [self sendGetRequest:@"index/join_black" param:param];
}
- (RACSignal *)searchUserWithWord:(NSString *)word {
    NSDictionary *param = @{@"word": word};
    RACSignal *signal = [self sendGetRequest:@"friend/select_friend" param:param];
    
    return [signal flattenMap:^__kindof RACSignal * _Nullable(id  _Nullable value) {
       
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            NSArray *dataDic = [value objectForKey:@"data"];
            [value setObject:dataDic.firstObject?:@{} forKey:@"data"];
//            if (dataDic.count > 0) {
//            }
            [subscriber sendNext:value];
            [subscriber sendCompleted];
            return nil;
        }];
    }];
}
- (RACSignal *)searchUserWithUid:(NSString *)user_id {
//    NSDictionary *param = @{@"user_id": user_id};
//    return [self sendGetRequest:@"user/user_info" param:param];
    return [self getFriendInfoWithId:user_id];
}
- (RACSignal *)addFriendWithId:(NSString *)ID nickname:(NSString *)nickname type:(NSString *)type content:(NSString *)content {
    NSDictionary *param = @{@"friend_id": ID,
                            @"nickname": nickname?:@"",
                            @"type": type
                            };
    if (content.isHaveStr) {
        param = [NSMutableDictionary dictionaryWithDictionary:param];
        ((NSMutableDictionary *)param)[@"content"] = content;
    }
    return [self sendGetRequest:@"friend/request_friend" param:param];
}
- (RACSignal *)agreeFriendRequestWithFriend_id:(NSString *)friend_id nickname:(NSString *)nickname type:(NSString *)type {
    NSDictionary *param = @{@"friend_id": friend_id,
                            @"nickname": nickname?:@"",
                            @"type": type
                            };
    return [self sendGetRequest:@"friend/add_friend" param:param];
}
- (RACSignal *)rejectFriendRequestWithFriend_id:(NSString *)friend_id {
    NSDictionary *param = @{@"friend_id": friend_id};
    return [self sendGetRequest:@"friend/refused_friend" param:param];

}
- (RACSignal *)getFriendInfoWithId:(NSString *)friend_id {
    NSDictionary *param = @{@"friend_id": friend_id};
    return [self sendGetRequest:@"friend/friend_info" param:param];
}
- (RACSignal *)setFriendRemarkWithId:(NSString *)friend_id nickname:(NSString *)nickname {
    NSDictionary *param = @{@"friend_id": friend_id,
                            @"nickname": nickname
                            };
    return [self sendGetRequest:@"friend/friend_ps" param:param];
}

- (RACSignal *)deleteFriendWithId:(NSString *)friend_id {
    NSDictionary *param = @{@"id": friend_id};
    return [self sendGetRequest:@"friend/del_friend" param:param];
}
- (RACSignal *)downloadFileWithUrlPath:(NSString *)urlPath {
    
    return [self downloadFileWithUrlPath:urlPath];
}

- (RACSignal *)reportUserWithUserid:(NSString *)friend_id content:(NSString *)content {
    NSDictionary *param = @{@"friend_id": friend_id,
                            @"content": content
                            };
    return [self sendGetRequest:@"index/report" param:param];
}
@end
